import Vue from 'vue'
import VueRouter from 'vue-router'
import Manager from '../views/Manager.vue'
import Home from '@/views/manager/Home.vue'

const originalPush = VueRouter.prototype.push
 
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: Manager,
    redirect: '/home',
    children: [
      { path: '403', name: 'Auth', meta: { name: '无权限'},component: () => import('@/views/manager/Auth.vue')},
      { path: 'home', name: 'Home', meta: { name: '系统首页'},component: () => import('@/views/manager/Home.vue')},
      { path: 'user', name: 'User', meta: { name: '用户信息'},component: () => import('@/views/manager/User.vue')},
      { path: 'person', name: 'Person', meta: { name: '个人信息'},component: () => import('@/views/manager/Person.vue')},
      { path: 'password', name: 'Password', meta: { name: '修改密码'},component: () => import('@/views/manager/Password.vue')},
      { path: 'news', name: 'News', meta: { name: '新闻信息'},component: () => import('@/views/manager/News.vue')},
      { path: 'newsDetails', name: 'newsDetails', meta: { name: '新闻详情'},component: () => import('@/views/manager/NewsDetails.vue')},
      { path: 'notice', name: 'Notice', meta: { name: '系统公告'},component: () => import('@/views/manager/Notice.vue')},
      { path: 'logs', name: 'Logs', meta: { name: '系统日志'},component: () => import('@/views/manager/Logs.vue')},
      { path: 'charts', name: 'Charts', meta: { name: '图表统计'},component: () => import('@/views/manager/Charts.vue')},
      { path: 'orders', name: 'Orders', meta: { name: '订单管理'},component: () => import('@/views/manager/Orders.vue')},
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: { name: '登录'},
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta: { name: '注册'},
    component: () => import('@/views/Register.vue')
  },
  {
    path: '*',
    name: '404',
    meta: { name: '无法访问'},
    component: () => import('@/views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  
  let admin = ['/user']
  let user = JSON.parse(localStorage.getItem('honey-user') || '{}')
  if (user.role != '管理员' && admin.includes(to.next)) {
    next('/403')
  }
   next()
})

export default router
